Total Complexity | 3 |
Total Lines | 35 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import { QueryHandler } from '@nestjs/cqrs'; |
||
9 | |||
10 | @QueryHandler(GetSchoolByIdQuery) |
||
11 | export class GetSchoolByIdQueryHandler { |
||
12 | constructor( |
||
13 | @Inject('ISchoolRepository') |
||
14 | private readonly schoolRepository: ISchoolRepository, |
||
15 | private readonly canUserAccessToSchool: CanUserAccessToSchool |
||
16 | ) {} |
||
17 | |||
18 | public async execute(query: GetSchoolByIdQuery): Promise<SchoolDetailView> { |
||
19 | const { id, userId } = query; |
||
20 | const school = await this.schoolRepository.findOneById(id); |
||
21 | |||
22 | if (!school) { |
||
23 | throw new SchoolNotFoundException(); |
||
24 | } |
||
25 | |||
26 | if (false === await this.canUserAccessToSchool.isSatisfiedBy(school, userId)) { |
||
27 | throw new UserCantAccessToSchoolException(); |
||
28 | } |
||
29 | |||
30 | return new SchoolDetailView( |
||
31 | school.getId(), |
||
32 | school.getName(), |
||
33 | school.getReference(), |
||
34 | school.getAddress(), |
||
35 | school.getCity(), |
||
36 | school.getZipCode(), |
||
37 | school.getStatus(), |
||
38 | school.getType(), |
||
39 | school.getEmail(), |
||
40 | school.getPhoneNumber(), |
||
41 | school.getNumberOfClasses(), |
||
42 | school.getNumberOfStudents(), |
||
43 | school.getObservation() |
||
44 | ); |
||
47 |